<?php

class ControllerModuleVvcounter extends Controller {
	protected function index() {
		//LOAD LANGUAGE
		$this->language->load('module/vvcounter');

		//SET TITLE
      	$this->data['heading_title'] = $this->language->get('heading_title');

		//LOAD MODEL FILES
		$this->load->model('module/vvcounter');

		$this->model_module_vvcounter->addVisitor($this->request->server['REMOTE_ADDR']);
		$results = $this->model_module_vvcounter->getTotalvisitors();
		
		/*$num_visitors = 0;
		$num_visits = 0;
		
		foreach ($results as $result) {
			$num_visits += $result['count'];
			$num_visitors++; 
		}*/
		$arrgetTotal = $this->getResult($results);
		$num_visits = $arrgetTotal[0]+1000000;
		$num_visitors = $arrgetTotal[1];
		
		
		$filler_text = "00000000";
		if (strlen("$num_visitors") < 8) {
			$num_visitors = substr($filler_text, 0, (8-strlen("$num_visitors"))) . "$num_visitors";
		}
		if (strlen("$num_visits") < 8) {
			$num_visits = substr($filler_text, 0, (8-strlen("$num_visits"))) . "$num_visits";
		}
		
		$arr_num_visits = $this->getDigits($num_visits,8);
		$arr_num_visitors = $this->getDigits($num_visitors,8);
		$str_visits = "";
		foreach ($arr_num_visits as $value){
			$str_visits .= $this->showDigitImage('default',$value);
		}
		$str_visitors = "";
		foreach ($arr_num_visitors as $value){
			$str_visitors .= $this->showDigitImage('default',$value);
		}
		
		//TODAY
		$today_begin = date("Y-m-d 00:00:00");
		$today_end = date("Y-m-d 23:59:59");
		
		//YESTERDAY
		$yesterday_begin = date("Y-m-d 00:00:00",mktime(0, 0, 0, date("m")  , date("d")-1, date("Y")));
		$yesterday_end = date("Y-m-d 23:59:59",mktime(0, 0, 0, date("m")  , date("d")-1, date("Y")));
		
		//THIS WEEK
		$thisweek_fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("n"), date("j"), date("Y")) - ((date("N")-1)*3600*24));
		$thisweek_toDate = 'CURRENT_TIMESTAMP';
		
		//LAST WEEK
		$lastweek_fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("n"), date("j")-6, date("Y")) - ((date("N"))*3600*24));
		$lastweek_toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, date("n"), date("j"), date("Y")) - ((date("N"))*3600*24));
		
		//THIS MONTH
		$thismonth_fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("m")  , 1, date("Y")));
		$thismonth_toDate = 'CURRENT_TIMESTAMP';
		
		//LAST MONTH
		$lastmonth_fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date("m")-1  , 1 , date("Y")));
		$lastmonth_toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, date("m"), date("d")-date("j"), date("Y")));
		
		//THIS YEAR
		$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, date(1)  , date(1), date("Y")));
		$toDate = 'CURRENT_TIMESTAMP';
		
		//LAST YEAR
		$fromDate = date("Y-m-d H:i:s",mktime(0, 0, 0, 1  , 1, date("Y")-1));
		$toDate = date("Y-m-d H:i:s",mktime(23, 59, 59, 12, 31, date("Y")-1));
		
		//LAST 1 minutes
		$current_begin = date("Y-m-d H:i:s",mktime(date("H"),date("i")-5, date("s"), date("m"),date("d"),date("Y")));
		$current_end = date("Y-m-d H:i:s");
						
		$counttoday = $this->model_module_vvcounter->getTotalguest($today_begin, $today_end);
		$countcurrent = $this->model_module_vvcounter->getTotalguest($current_begin, $current_end);
		$countyesterday = $this->model_module_vvcounter->getTotalguest($yesterday_begin, $yesterday_end);
		$countthisweek = $this->model_module_vvcounter->getTotalguest($thisweek_fromDate, $thisweek_toDate);
		$countlastweek = $this->model_module_vvcounter->getTotalguest($lastweek_fromDate, $lastweek_toDate);
		$countthismonth = $this->model_module_vvcounter->getTotalguest($thismonth_fromDate, $thismonth_toDate);
		$countlastmonth = $this->model_module_vvcounter->getTotalguest($lastmonth_fromDate, $lastmonth_toDate);
	
		$this->data['num_visits'] = $str_visits;
		$this->data['num_visitors'] = $str_visitors;
		$this->data['bg_color'] = $this->config->get('vvcounter_background_color');
		$this->data['font_color'] = $this->config->get('vvcounter_font_color');
		
		$arr_today = $this->getResult($counttoday);
		$this->data['num_visits_today'] = $arr_today[0];
		$this->data['num_visitors_today'] = $arr_today[1];
		
		$arr_current = $this->getResult($countcurrent);
		$this->data['num_visits_current'] = $arr_current[0];
		$this->data['num_visitors_current'] = $arr_current[1];
		
		$arr_yesterday = $this->getResult($countyesterday);
		$this->data['num_visits_yesterday'] = $arr_yesterday[0];
		$this->data['num_visitors_yesterday'] = $arr_yesterday[1];
		
		$arr_thisweek = $this->getResult($countthisweek);
		$this->data['num_visits_thisweek'] = $arr_thisweek[0];
		$this->data['num_visitors_thisweek'] = $arr_thisweek[1];
		
		$arr_lastweek = $this->getResult($countlastweek);
		$this->data['num_visits_lastweek'] = $arr_lastweek[0];
		$this->data['num_visitors_lastweek'] = $arr_lastweek[1];
		
		$arr_thismonth = $this->getResult($countthismonth);
		$this->data['num_visits_thismonth'] = $arr_thismonth[0];
		$this->data['num_visitors_thismonth'] = $arr_thismonth[1];
		
		$arr_lastmonth = $this->getResult($countlastmonth);
		$this->data['num_visits_lastmonth'] = $arr_lastmonth[0];
		$this->data['num_visitors_lastmonth'] = $arr_lastmonth[1];
	
		if ($this->config->get('vvcounter_count_type') == 1) {
			$this->data['vvcounter_num'] = "$num_visitors";
		}
		else {
			$this->data['vvcounter_num'] = "$num_visits";
		}
		$this->id = 'vvcounter';


		//CHOOSE TEMPLATE
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/vvcounter.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/vvcounter.tpl';
		} else {
			$this->template = 'default/template/module/vvcounter.tpl';
		}

		//RENDER
		$this->render();
	}
	
	function getDigits( $number, $length=0 )
	{
		$strlen = strlen($number);
		
		$arr	=	array();
		$diff	=	$length -  $strlen;
		
		// Push Leading Zeros
		while ( $diff>0 ){
			array_push( $arr,0 );
			$diff--;
		}
		
		// For PHP 5.x: 
		$arrNumber	=	str_split( $number );
		
		$arr		=	array_merge( $arr,$arrNumber );
		
		return $arr;
	}
	
	function showDigitImage( $digit_type="default", $digit )
	{	
		$ret	=	'<img src="image/digitimage/'.$digit_type.'/'.$digit.'.png"';
		$ret	.=	' alt="mod_vvisit_counter"';
		$ret	.=	' title="Visitor $digit"';
		$ret	.=	' />';
		
		return $ret;
	}
	
	function getResult($arr){
		$num_visitors = 0;
		$num_visits = 0;
		foreach ($arr as $result) {
			$num_visits += $result['count'];
			$num_visitors++; 
		}
		return array( $num_visits, $num_visitors);
	}
}
?>